﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ModBot.ConfigBuilder.Forms
{
    public enum TargetTypeOption
    {
        Heals,
        Buffs,
        Debuffs
    }

    public partial class TargetTypeBuilderForm : Form
    {
        

        public Boolean IsCancelled { get; set; }

        private TargetTypeOption option = TargetTypeOption.Heals;
        public TargetTypeOption TargetTypeOption
        {
            get { return this.option; }
            set
            {
                if (this.option != value)
                {
                    this.option = value;
                    UpdateTargetTypeOptions();
                }
            }
        }


        public TargetTypeBuilderForm()
        {
            InitializeComponent();
            this.IsCancelled = true;
            UpdateTargetTypeOptions();
        }

        Boolean classTextboxFollowsCheckbox = false;
        private void UpdateTargetTypeOptions()
        {
            ResetAllFields();
            DisableAllFields();

            switch (option)
            {
                case TargetTypeOption.Heals:
                    pcCheckbox.Enabled = true;
                    petCheckbox.Enabled = true;
                    groupCheckbox.Enabled = true;
                    hpCheckbox.Enabled = true;

                    EnableClassCheckboxes();
                    classTextboxFollowsCheckbox = true;

                    tntCheckbox.Enabled = true;
                    myPetCheckbox.Enabled = true;
                    selfCheckbox.Enabled = true;                  
                   
                    break;
                case TargetTypeOption.Buffs:
                    EnableClassCheckboxes();
                    classTextboxFollowsCheckbox = false;

                    selfCheckbox.Enabled = true;
                    groupCheckbox.Enabled = true;
                    petCheckbox.Enabled = true;
                    myPetCheckbox.Enabled = true;
                    cbtCheckbox.Enabled = true;
                    idleCheckbox.Enabled = true;
                    auraCheckbox.Enabled = true;
                    tankCheckbox.Enabled = true;
                    break;
                case TargetTypeOption.Debuffs:

                    break;
            }
        }

        #region Checkbox Interaction

        private void warCheckbox_CheckedChanged(object sender, EventArgs e)
        {
            if(classTextboxFollowsCheckbox) warTextbox.Enabled = warCheckbox.Checked;
            UpdatePreview();
        }

        private void shdCheckbox_CheckedChanged(object sender, EventArgs e)
        {
            if (classTextboxFollowsCheckbox) shdTextbox.Enabled = shdCheckbox.Checked;
            UpdatePreview();
        }

        private void palCheckbox_CheckedChanged(object sender, EventArgs e)
        {
            if (classTextboxFollowsCheckbox) palTextbox.Enabled = palCheckbox.Checked;
            UpdatePreview();
        }

        private void rngCheckbox_CheckedChanged(object sender, EventArgs e)
        {
            if (classTextboxFollowsCheckbox) rngTextbox.Enabled = rngCheckbox.Checked;
            UpdatePreview();
        }

        private void mnkCheckbox_CheckedChanged(object sender, EventArgs e)
        {
            if (classTextboxFollowsCheckbox) mnkTextbox.Enabled = mnkCheckbox.Checked;
            UpdatePreview();
        }

        private void rogCheckbox_CheckedChanged(object sender, EventArgs e)
        {
            if (classTextboxFollowsCheckbox) rogTextbox.Enabled = rogCheckbox.Checked;
            UpdatePreview();
        }

        private void brdCheckbox_CheckedChanged(object sender, EventArgs e)
        {
            if (classTextboxFollowsCheckbox) brdTextbox.Enabled = brdCheckbox.Checked;
            UpdatePreview();
        }

        private void bstCheckbox_CheckedChanged(object sender, EventArgs e)
        {
            if (classTextboxFollowsCheckbox) bstTextbox.Enabled = bstCheckbox.Checked;
            UpdatePreview();
        }

        private void berCheckbox_CheckedChanged(object sender, EventArgs e)
        {
            if (classTextboxFollowsCheckbox) berTextbox.Enabled = berCheckbox.Checked;
            UpdatePreview();
        }

        private void shmCheckbox_CheckedChanged(object sender, EventArgs e)
        {
            if (classTextboxFollowsCheckbox) shmTextbox.Enabled = shmCheckbox.Checked;
            UpdatePreview();

        }

        private void clrCheckbox_CheckedChanged(object sender, EventArgs e)
        {
            if (classTextboxFollowsCheckbox) clrTextbox.Enabled = clrCheckbox.Checked;
            UpdatePreview();
        }

        private void druCheckbox_CheckedChanged(object sender, EventArgs e)
        {
            if (classTextboxFollowsCheckbox) druTextbox.Enabled = druCheckbox.Checked;
            UpdatePreview();
        }

        private void wizCheckbox_CheckedChanged(object sender, EventArgs e)
        {
            if (classTextboxFollowsCheckbox) wizTextbox.Enabled = wizCheckbox.Checked;
            UpdatePreview();
        }

        private void magCheckbox_CheckedChanged(object sender, EventArgs e)
        {
            if (classTextboxFollowsCheckbox) magTextbox.Enabled = magCheckbox.Checked;
            UpdatePreview();
        }

        private void encCheckbox_CheckedChanged(object sender, EventArgs e)
        {
            if (classTextboxFollowsCheckbox) encTextbox.Enabled = encCheckbox.Checked;
            UpdatePreview();
        }

        private void necCheckbox_CheckedChanged(object sender, EventArgs e)
        {
            if (classTextboxFollowsCheckbox) necTextbox.Enabled = necCheckbox.Checked;
            UpdatePreview();
        }

        private void hpCheckbox_CheckedChanged(object sender, EventArgs e)
        {
            if (classTextboxFollowsCheckbox) hpTextbox.Enabled = hpCheckbox.Checked;
            UpdatePreview();
        }

        private void petCheckbox_CheckedChanged(object sender, EventArgs e)
        {
            /*if (myPetCheckbox.Checked && petCheckbox.Checked)
            {
                myPetCheckbox.Checked = false;
            }*/

            UpdatePreview();
        }

        private void myPetCheckbox_CheckedChanged(object sender, EventArgs e)
        {
            /*if (myPetCheckbox.Checked && petCheckbox.Checked)
            {
                petCheckbox.Checked = false;
            }*/
            UpdatePreview();
        }

        private void petSpellCheckbox_CheckedChanged(object sender, EventArgs e)
        {
            if (petSpellCheckbox.Checked)
            {
                ResetAllFields();
                DisableAllFields();
                petSpellCheckbox.Checked = true;
            }
            else
            {
                UpdateTargetTypeOptions();
            }
            
            UpdatePreview();
        }        

        private void selfCheckbox_CheckedChanged(object sender, EventArgs e)
        {
            UpdatePreview();
        }

        private void tntCheckbox_CheckedChanged(object sender, EventArgs e)
        {
            UpdatePreview();
        }

        private void pcCheckbox_CheckedChanged(object sender, EventArgs e)
        {
            UpdatePreview();
        }

        private void groupCheckbox_CheckedChanged(object sender, EventArgs e)
        {
            UpdatePreview();
        }

        private void idleCheckbox_CheckedChanged(object sender, EventArgs e)
        {
            UpdatePreview();
        }

        private void cbtCheckbox_CheckedChanged(object sender, EventArgs e)
        {
            UpdatePreview();
        }

        private void tankCheckbox_CheckedChanged(object sender, EventArgs e)
        {
            UpdatePreview();
        }

        private void auraCheckbox_CheckedChanged(object sender, EventArgs e)
        {
            UpdatePreview();
        }

       


        #endregion

        private void ResetAllFields()
        {
            pcCheckbox.Checked = false;
            groupCheckbox.Checked = false;
            petCheckbox.Checked = false;
            myPetCheckbox.Checked = false;
            tntCheckbox.Checked = false;
            selfCheckbox.Checked = false;
            tankCheckbox.Checked = false;
            cbtCheckbox.Checked = false;
            idleCheckbox.Checked = false;

            hpCheckbox.Checked = false;
            hpTextbox.Text = string.Empty;

            warCheckbox.Checked = false;
            warTextbox.Text = string.Empty;

            shdCheckbox.Checked = false;
            shdTextbox.Text = string.Empty;

            palCheckbox.Checked = false;
            palTextbox.Text = string.Empty;

            rngCheckbox.Checked = false;
            rngTextbox.Text = string.Empty;

            mnkCheckbox.Checked = false;
            mnkTextbox.Text = string.Empty;

            rogCheckbox.Checked = false;
            rogTextbox.Text = string.Empty;

            brdCheckbox.Checked = false;
            brdTextbox.Text = string.Empty;

            bstCheckbox.Checked = false;
            bstTextbox.Text = string.Empty;

            berCheckbox.Checked = false;
            berTextbox.Text = string.Empty;

            shmCheckbox.Checked = false;
            shmTextbox.Text = string.Empty;

            clrCheckbox.Checked = false;
            clrTextbox.Text = string.Empty;

            druCheckbox.Checked = false;
            druTextbox.Text = string.Empty;

            wizCheckbox.Checked = false;
            wizTextbox.Text = string.Empty;

            magCheckbox.Checked = false;
            magTextbox.Text = string.Empty;

            encCheckbox.Checked = false;
            encTextbox.Text = string.Empty;

            necCheckbox.Checked = false;
            necTextbox.Text = string.Empty;
        }

        private void DisableAllFields()
        {
            pcCheckbox.Enabled = false;
            groupCheckbox.Enabled = false;
            petCheckbox.Enabled = false;
            myPetCheckbox.Enabled = false;
            tntCheckbox.Enabled = false;
            selfCheckbox.Enabled = false;

            tankCheckbox.Enabled = false;
            cbtCheckbox.Enabled = false;
            idleCheckbox.Enabled = false;

            hpCheckbox.Enabled  = false;
            hpTextbox.Enabled = false;
            auraCheckbox.Enabled = false;

            DisableClassCheckboxes();
            DisableClassTextboxes();
        }

        private void EnableAllFields()
        {
            pcCheckbox.Enabled = true;
            groupCheckbox.Enabled = true;
            petCheckbox.Enabled = true;
            myPetCheckbox.Enabled = true;
            tntCheckbox.Enabled = true;
            selfCheckbox.Enabled = true;

            tankCheckbox.Enabled = true;
            cbtCheckbox.Enabled = true;
            idleCheckbox.Enabled = true;
            auraCheckbox.Enabled = true;

            hpCheckbox.Enabled = true;
            hpTextbox.Enabled = hpCheckbox.Checked;
            
            EnableClassCheckboxes();
            EnableClassTextboxes();            
        }

        private void EnableClassTextboxes()
        {           
            warTextbox.Enabled = warCheckbox.Checked;
            shdTextbox.Enabled = shdCheckbox.Checked;
            palTextbox.Enabled = palCheckbox.Checked;
            rngTextbox.Enabled = rngCheckbox.Checked;
            mnkTextbox.Enabled = mnkCheckbox.Checked;
            rogTextbox.Enabled = rogCheckbox.Checked;
            brdTextbox.Enabled = brdCheckbox.Checked;
            bstTextbox.Enabled = bstCheckbox.Checked;
            berTextbox.Enabled = berCheckbox.Checked;
            shmTextbox.Enabled = shmCheckbox.Checked;
            clrTextbox.Enabled = clrCheckbox.Checked;
            druTextbox.Enabled = druCheckbox.Checked;
            wizTextbox.Enabled = wizCheckbox.Checked;
            magTextbox.Enabled = magCheckbox.Checked;
            encTextbox.Enabled = encCheckbox.Checked;
            necTextbox.Enabled = necCheckbox.Checked;

        }

        private void EnableClassCheckboxes()
        {
            warCheckbox.Enabled = true;
            shdCheckbox.Enabled = true;
            palCheckbox.Enabled = true;
            rngCheckbox.Enabled = true;
            mnkCheckbox.Enabled = true;
            rogCheckbox.Enabled = true;
            brdCheckbox.Enabled = true;
            bstCheckbox.Enabled = true;
            berCheckbox.Enabled = true;
            shmCheckbox.Enabled = true;
            clrCheckbox.Enabled = true;
            druCheckbox.Enabled = true;
            wizCheckbox.Enabled = true;
            magCheckbox.Enabled = true;
            encCheckbox.Enabled = true;
            necCheckbox.Enabled = true;            
        }

        private void DisableClassTextboxes()
        {
            warTextbox.Enabled = false;
            shdTextbox.Enabled = false;
            palTextbox.Enabled = false;
            rngTextbox.Enabled = false;
            mnkTextbox.Enabled = false;
            rogTextbox.Enabled = false;
            brdTextbox.Enabled = false;
            bstTextbox.Enabled = false;
            berTextbox.Enabled = false;
            shmTextbox.Enabled = false;
            clrTextbox.Enabled = false;
            druTextbox.Enabled = false;
            wizTextbox.Enabled = false;
            magTextbox.Enabled = false;
            encTextbox.Enabled = false;
            necTextbox.Enabled = false;

        }

        private void DisableClassCheckboxes()
        {
            warCheckbox.Enabled = false;
            shdCheckbox.Enabled = false;
            palCheckbox.Enabled = false;
            rngCheckbox.Enabled = false;
            mnkCheckbox.Enabled = false;
            rogCheckbox.Enabled = false;
            brdCheckbox.Enabled = false;
            bstCheckbox.Enabled = false;
            berCheckbox.Enabled = false;
            shmCheckbox.Enabled = false;
            clrCheckbox.Enabled = false;
            druCheckbox.Enabled = false;
            wizCheckbox.Enabled = false;
            magCheckbox.Enabled = false;
            encCheckbox.Enabled = false;
            necCheckbox.Enabled = false;
        }

        #region Textbox Interaction
        private void warTextbox_TextChanged(object sender, EventArgs e)
        {
            UpdatePreview();
        }

        private void shdTextbox_TextChanged(object sender, EventArgs e)
        {
            UpdatePreview();
        }

        private void palTextbox_TextChanged(object sender, EventArgs e)
        {
            UpdatePreview();
        }

        private void rngTextbox_TextChanged(object sender, EventArgs e)
        {
            UpdatePreview();
        }

        private void mnkTextbox_TextChanged(object sender, EventArgs e)
        {
            UpdatePreview();
        }

        private void rogTextbox_TextChanged(object sender, EventArgs e)
        {
            UpdatePreview();
        }

        private void brdTextbox_TextChanged(object sender, EventArgs e)
        {
            UpdatePreview();
        }

        private void bstTextbox_TextChanged(object sender, EventArgs e)
        {
            UpdatePreview();
        }

        private void berTextbox_TextChanged(object sender, EventArgs e)
        {
            UpdatePreview();
        }

        private void shmTextbox_TextChanged(object sender, EventArgs e)
        {
            UpdatePreview();
        }

        private void clrTextbox_TextChanged(object sender, EventArgs e)
        {
            UpdatePreview();
        }

        private void druTextbox_TextChanged(object sender, EventArgs e)
        {
            UpdatePreview();
        }

        private void wizTextbox_TextChanged(object sender, EventArgs e)
        {
            UpdatePreview();
        }

        private void magTextbox_TextChanged(object sender, EventArgs e)
        {
            UpdatePreview();
        }

        private void enchTextbox_TextChanged(object sender, EventArgs e)
        {
            UpdatePreview();
        }

        private void necTextbox_TextChanged(object sender, EventArgs e)
        {
            UpdatePreview();
        }

        private void hpTextbox_TextChanged(object sender, EventArgs e)
        {
            UpdatePreview();
        }


        #endregion        

        private void UpdatePreview()
        {
            previewLabel.Text = GetTargetTypeText();

            UpdateWarning();

            Application.DoEvents();
        }

        public string GetTargetTypeText()
        {
            string targetTypeText = string.Empty;
            
            if (pcCheckbox.Checked) targetTypeText += "pc ";
            if (petCheckbox.Checked) targetTypeText += "pet ";
            if (myPetCheckbox.Checked) targetTypeText += "mypet ";
            targetTypeText += GetCheckboxWithTextbox(hpCheckbox, hpTextbox);


            if (tankCheckbox.Checked) targetTypeText += "tank ";
            if (petSpellCheckbox.Checked) return "petspell";
            if (tntCheckbox.Checked) targetTypeText += "tnt ";
            if (groupCheckbox.Checked)
            {
                if (option == TargetTypeOption.Heals)
                {
                    targetTypeText += "group ";           
                }
                else
                {
                    targetTypeText += "grp ";           
                }
            }            

            if (selfCheckbox.Checked) targetTypeText += "self ";
            if (cbtCheckbox.Checked) targetTypeText += "cbt ";
            if (idleCheckbox.Checked) targetTypeText += "idle ";
            if (auraCheckbox.CanFocus) targetTypeText += "aura ";

            targetTypeText += GetCheckboxWithTextbox(warCheckbox, warTextbox);
            targetTypeText += GetCheckboxWithTextbox(shdCheckbox, shdTextbox);
            targetTypeText += GetCheckboxWithTextbox(palCheckbox, palTextbox);
            targetTypeText += GetCheckboxWithTextbox(rngCheckbox, rngTextbox);

            targetTypeText += GetCheckboxWithTextbox(mnkCheckbox, mnkTextbox);
            targetTypeText += GetCheckboxWithTextbox(rogCheckbox, rogTextbox);
            targetTypeText += GetCheckboxWithTextbox(brdCheckbox, brdTextbox);
            targetTypeText += GetCheckboxWithTextbox(bstCheckbox, bstTextbox);

            targetTypeText += GetCheckboxWithTextbox(berCheckbox, berTextbox);
            targetTypeText += GetCheckboxWithTextbox(shmCheckbox, shmTextbox);
            targetTypeText += GetCheckboxWithTextbox(clrCheckbox, clrTextbox);
            targetTypeText += GetCheckboxWithTextbox(druCheckbox, druTextbox);

            targetTypeText += GetCheckboxWithTextbox(wizCheckbox, wizTextbox);
            targetTypeText += GetCheckboxWithTextbox(magCheckbox, magTextbox);
            targetTypeText += GetCheckboxWithTextbox(encCheckbox, encTextbox);
            targetTypeText += GetCheckboxWithTextbox(necCheckbox, necTextbox);

            return targetTypeText.Trim();        
        }

        public void SetTargetTypeSelections(string targetTypeText, char delimeter)
        {
            string[] targetTypes = targetTypeText.Split(delimeter);

            for (int i = 0; i < targetTypes.Length; i++)
            {
                string targetType = targetTypes[i];
                if(targetType.ToUpper().StartsWith("HP")) targetType = "HP";
                switch(targetType.Substring(0, Math.Min(targetType.Length, 3)).ToUpper())
                {
                    case "PC":
                        pcCheckbox.Checked = true;
                        break;
                    case "PET":
                        petCheckbox.Checked = true;
                        break;
                    case "MYP":
                        myPetCheckbox.Checked = true;
                        break;
                    case "HP":
                        hpCheckbox.Checked = true;
                        hpTextbox.Text = targetTypes[i].ToUpper().Replace("HP", "");
                        break;
                    
                    case "TAN":
                        tankCheckbox.Checked = true;
                        break;
                    case "PETS":
                        petSpellCheckbox.Checked = true;
                        break;
                    case "TNT":
                        tntCheckbox.Checked = true;
                        break;
                    case "GRP":
                        groupCheckbox.Checked = true;
                        break;

                    case "SEL":
                        selfCheckbox.Checked = true;
                        break;
                    case "AUR":
                        auraCheckbox.Checked = true;
                        break;
                    case "CBT":
                        cbtCheckbox.Checked = true;
                        break;

                    case "IDL":
                        idleCheckbox.Checked = true;
                        break;

                    default:
                        ParseClassAndValue(targetType);
                        break;

                }
            }



        }

        private void ParseClassAndValue(string input)
        {
            if(input.Length < 3) throw new InvalidOperationException("Error input was not of correct format. Expected at least 3 characters and received " + input.Length);

            string className = input.Substring(0, 3);
            string value = (input.Length > 3 ? input.Replace(className, "") : string.Empty);

            CheckBox classCheckbox = null;
            TextBox classTextbox = null;
            switch(className.ToUpper())
            {
                case "ENC":
                    classCheckbox =encCheckbox;
                    classTextbox =encTextbox;
                    break;                
                case "WIZ":
                    classCheckbox = wizCheckbox;
                    classTextbox =wizTextbox;
                    break;
                case "MAG":
                    classCheckbox =magCheckbox;
                    classTextbox =magTextbox;
                    break;
                case "NEC":
                    classCheckbox = necCheckbox;
                    classTextbox =necTextbox;
                    break;
                case "CLR":
                    classCheckbox = clrCheckbox;
                    classTextbox =clrTextbox;
                    break;
                case "DRU":
                    classCheckbox = druCheckbox;
                    classTextbox =druTextbox;
                    break;
                case "SHM":
                    classCheckbox = shmCheckbox;
                    classTextbox = shmTextbox;
                    break;
                case "PAL":
                    classCheckbox = palCheckbox;
                    classTextbox = palTextbox;
                    break;
                case "SHD":
                    classCheckbox = shdCheckbox;
                    classTextbox = shdTextbox;
                    break;
                case "WAR":
                    classCheckbox = warCheckbox;
                    classTextbox = warTextbox;
                    break;
                case "BST":
                    classCheckbox = bstCheckbox;
                    classTextbox = bstTextbox;
                    break;
                case "RNG":
                    classCheckbox =rngCheckbox;
                    classTextbox =rngTextbox;
                    break;
                case "BER":
                    classCheckbox = berCheckbox;
                    classTextbox = berTextbox;
                    break;
                case "ROG":
                    classCheckbox = rogCheckbox;
                    classTextbox = rogTextbox;
                    break;
                case "BRD":
                    classCheckbox = brdCheckbox;
                    classTextbox = brdTextbox;
                    break;
                case "MNK":
                    classCheckbox = mnkCheckbox;
                    classTextbox = mnkTextbox;
                    break;


            }

            classCheckbox.Checked = true;
            classTextbox.Text = value;
        }

        private string GetCheckboxWithTextbox(CheckBox box, TextBox text)
        {
            string output = string.Empty;
            if (box.Checked)
            {
                output = box.Text.ToLower();
                if(text.Text.Length > 0) output += text.Text;
            }
            return output.Trim() + " ";
        }

        private void UpdateWarning()
        {
            StringBuilder warningString = new StringBuilder();           

            const string selfText = "- Specifying both self and your bot's class could result in error.";
            const string petText = "- Specifying both pet and my pet could result in error.";
            
            Boolean petAndMyPetSelected = (petCheckbox.Checked && myPetCheckbox.Checked);
            Boolean selfSelected = selfCheckbox.Checked;

            if (selfSelected || petAndMyPetSelected)
            {
                warningString.AppendLine("!! Warning !!");
                if (selfSelected) warningString.AppendLine(selfText);
                if (petAndMyPetSelected) warningString.AppendLine(petText);               
            }

            warningLabel.Text = warningString.ToString();
        }

        private void okButton_Click(object sender, EventArgs e)
        {
            this.IsCancelled = false;
            this.Close();
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.IsCancelled = true;
            this.Close();
        }

       
      
        
    }
}
